import * as echarts from '../lib/ec-canvas/echarts';
var t = getApp()
let chart = null;
function initChart(canvas, width, height, dpr,arr) {
  chart = echarts.init(canvas, null, {
    width: width,
    height: height,
    devicePixelRatio: dpr // new
  });
  canvas.setChart(chart);

  var option1 = {
    title: {
      text: '',
      left: 'center'
    },
    legend: {
      data: ['热度', '正面', '负面'],
      orient: 'horizontal',
      x:'center',
      y:'bottom'
    },
    grid: {
      top: 15,   // 等价于 y: '16%'
      left: 15, 
      right: 15,
      bottom: 15,
      containLabel: true
  },
    tooltip: {
      show: true,
      trigger: 'axis'
    },
    xAxis: {
      type: 'category',
      boundaryGap: true,
      axisLabel: {
        rotate: 0, // 旋转角度
        interval: 0  //设置X轴数据间隔几个显示一个，为0表示都显示
        },
      data: [],
    },
    yAxis: {
      type: 'value',
      minInterval: 1,
      show:false,
      axisLabel : {
          formatter: function(){
              return "";
          }
      }

    },
    series: [{
      type: 'bar',
      smooth: true,
      barWidth : 20,
      showAllSymbol: false,
      axisTick:{
        show:false
      },
      name: '客流量',
      areaStyle: {
        normal: {color: '#d9ecfa'}
     },
     itemStyle : {  
        normal : {  
            lineStyle:{  
                color:'#0ab5f5'  
            }  
        }  
    },
      data: []
    }]
  };

  chart.setOption(option1);
  return chart;
}
Page({
  data: {
    bannar:['/images/index_bannar.png','/images/index_bannar.png','/images/index_bannar.png'],
    store_id:'',
    list:[],
    iconsUrl:t.globalData.iconsUrl,
    ec: {
      onInit: initChart
    },
    navHeight:t.globalData.navHeight,
    title:'门店详情',
    // 获取手机号弹窗参数
    show: false,
    duration: 300, //动画时长，单位毫秒
    position: 'bottom',
    round: true,  //是否显示圆角
    overlay: true, //是否显示遮罩层
    customStyle: '',//自定义弹出层样式
    overlayStyle: '' //自定义遮罩层样式
  },
  onLoad: function (options) {
    this.setData({
      store_id:options.store_id,
      dis:options.dis
    })
  },
  onShow: function () {
    this.getInfo()
    //判断手机是否登录了
    // if (t.globalData.show_tel_login) {
    //   this.showTelLogin()
    //   return;
    // }
  },
  // 显示电话登录的弹窗
  showTelLogin(){
    this.setData({show: true})
  },
  // 手机号 暂不登录按钮
  exit() {
    this.setData({show: false})
    wx.switchTab({
      url: '/pages/index/index',
    })
  },
  //电话登录
  getPhoneNumber(e){
    const that = this,
      iv = e.detail.iv,
      da = e.detail.encryptedData;
    if (!e.detail.encryptedData) return wx.showToast({ title: '授权失败,请重新授权！',icon:'none'})
    // 请求接口
    wx.login({
      success(e){
        console.log(e);
        wx.showLoading({
          title: '请稍后',
        })
        t.request('/login/getPhoneNumber',{code: e.code,iv:iv,encryptedData:da},function(event){
          wx.hideLoading({})
          if (event.code!==200) return wx.showToast({ title: event.msg,icon:'none' })
          if (event.code==200) {
            wx.setStorageSync('tel', event.result.phoneNumber)
            t.globalData.show_tel_login =false
            that.setData({show: false})
          }
        })
      }
    })
  },
  onRefresh(){
    //在当前页面显示导航条加载动画
    wx.showNavigationBarLoading(); 
    //显示 loading 提示框。需主动调用 wx.hideLoading 才能关闭提示框
    this.getInfo()
  },
  onPullDownRefresh: function () {
    //调用刷新时将执行的方法
    this.onRefresh();
    setTimeout(function () {
      // 不加这个方法真机下拉会一直处于刷新状态，无法复位
      wx.stopPullDownRefresh()
    }, 1000)
  },
  getInfo(e){
    var that = this
    t.request('/store/getOne',{store_id:this.data.store_id},function(res){
      console.log(res);
      if (res.code==200) {
        var list = res.result
        that.setData({
          list:list
        })
        // 如果有余额，判断是否绑定车辆
        if(res.result.balance != 0 && res.result.cars == 0){
          that.login = that.selectComponent("#login")
          that.login.show_login()
        }
        //list.in
        console.log(list.time);
        setTimeout(() => {
          chart.setOption({
            xAxis: {
              data: list.time,
            },
            series: [{
              data: list.in,
              itemStyle: {   
                  normal:{  
    　　　　　　　　　　　　//每个柱子的颜色即为colorList数组里的每一项，如果柱子数目多于colorList的长度，则柱子颜色循环使用该数组
                      color: function (params){
                          var colorList = list.colour;
                          return colorList[params.dataIndex];
                      }
                  },
              },
            }]
          })
        }, 1000);
      }
    })
  },
  call(e){
    wx.makePhoneCall({
      phoneNumber: this.data.list.tel,
    }).catch((e) => {
      console.log(e) 
    })
  
  },
  invest(e){
     if (t.globalData.show_tel_login) {
      this.showTelLogin()
      return;
    }
    wx.navigateTo({
      url: '/pages/personCenter/chooseStore/invest/invest?store_id=' + this.data.store_id ,
    })
  },
  to_map(e){
    if (t.globalData.show_tel_login) {
      this.showTelLogin()
      return;
    }
    var list=this.data.list
    wx.getLocation({
      type: 'wgs84', 
      success: function (res) {
        wx.openLocation({
          latitude: +list.lat,
          longitude: +list.lng,
          name: list.title,
          address: list.address
        })
      }
    })
  },
})